import { Body, Controller, Get, Post, Req } from '@nestjs/common';
import { ApiOkResponse, ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger';
import { ADMIN_PREFIX } from '../../admin.constants';
import { AdminUser } from '../../core/decorators/admin-user.decorator';
import { BaseShiftService } from './shift.service';
import { FastifyRequest } from 'fastify';

@ApiSecurity(ADMIN_PREFIX)
@ApiTags('班组管理模块')
@Controller('shift')
@Controller()
export class BaseShiftController {
  constructor(private shiftService: BaseShiftService) // private menuService: SysMenuService,
  {}

  // 获取班组列表
  @ApiOperation({ summary: '获取班组列表' })
  @ApiOkResponse()
  @Get('list')
  async list() {
    return await this.shiftService.list();
  }

  // 分页查询班组信息
  @ApiOperation({ summary: '分页获取获取班组列表' })
  @ApiOkResponse()
  @Post('page')
  async page(@Body() dto) {
    const [list, total] = await this.shiftService.page(dto);

    return {
      list,
      pagination: {
        total,
        pageNum: dto.pageNum,
        pageSize: dto.pageSize,
      },
    };
  }

  // 新增班组
  @ApiOperation({ summary: '添加班组' })
  @ApiOkResponse()
  @Post('add')
  async add(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;
    await this.shiftService.isExistKey(dto.shiftCode);

    if (dto.userList && dto.userList.length > 0) {
      await this.shiftService.isExistKey1(dto.userList, dto.shiftCode);
    }

    await this.shiftService.add(dto, user, mac, ip, syncHostSqlite);
  }

  // 更新班组
  @ApiOperation({ summary: '更新班组' })
  @ApiOkResponse()
  @Post('update')
  async update(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;
    await this.shiftService.update(dto, user, mac, ip, syncHostSqlite);
  }

  // 删除班组
  @ApiOperation({ summary: '删除班组' })
  @ApiOkResponse()
  @Post('delete')
  async delete(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;
    await this.shiftService.delete(dto, user, mac, ip, syncHostSqlite);
  }

  // 获取班组详情
  @ApiOperation({ summary: '获取班组详情' })
  @ApiOkResponse()
  @Post('info')
  async info(@Body() dto) {
    return await this.shiftService.info(dto.id);
  }
}
